package com.cdjf.controller;

import com.cdjf.pojo.Address;
import com.cdjf.pojo.Result;
import com.cdjf.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public Result Hello(
            @RequestParam(name = "name", required = false) String username,
            Integer age
    ) {
        System.out.println("Hello world ~ name=" + username + ", age=" + age);
        return Result.success();
    }

    @RequestMapping("/simplePojo")
    public Result simplePojo(User user) {
        System.out.println("Hello world ~ + " + user);
        return Result.success();
    }

    @RequestMapping("/arrayParam")
    public Result arrayParam(String[] hobby) {
        System.out.println("arrayParam: " + Arrays.toString(hobby));
        return Result.success();
    }

    @RequestMapping("/dateParam")
    public Result dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime localTime) {
        System.out.println("localTime:" + localTime);
        return Result.success();
    }

    @RequestMapping("/jsonParam")
    public Result jsonParam(@RequestBody User user) {
        System.out.println("jsonParam:" + user);
        ArrayList<Address> list = new ArrayList<>();
        Address address = new Address("成都", "四川");
        Address address2 = new Address("南充", "四川");
        list.add(address);
        list.add(address2);
        return Result.success(list);
    }

    @RequestMapping("/path/{id}/{name}")
    public Result pathParam(@PathVariable Integer id, @PathVariable String name) {
        System.out.println("path id:" + id + ", name:" + name);
        return Result.success(new Address("成都", "四川"));
    }


}
